﻿using System;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using RaGE.Properties;

namespace RaGE
{
    public partial class MainWindow
    {
        private void ButtonAddPathClick(object sender, EventArgs e)
        {
            var pObj = new PathObject();

            pObj.ID = int.Parse(textBox_PathID.Text);

            pObj.NameLen = 0;
            pObj.Name = Encoding.Default.GetBytes(textBox_Path.Text);

            path.Add(pObj);

            listView_Path.Items.Add(new ListViewItem(new[]
            {
                pObj.ID.ToString(CultureInfo.InvariantCulture),
                textBox_Path.Text
            }));

            listView_Path.Items[listView_Path.Items.Count - 1].EnsureVisible();
        }

        private void ButtonDeletePathClick(object sender, EventArgs e)
        {
            if (listView_Path.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectPathFirst);
            else
            {
                path.RemoveAt(listView_Path.SelectedItems[0].Index);
                listView_Path.Items.RemoveAt(listView_Path.SelectedItems[0].Index);
            }
        }

        private void ButtonSavePathClick(object sender, EventArgs e)
        {
            if (listView_Path.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectPathFirst);
            else
            {
                path[listView_Path.SelectedItems[0].Index].ID = int.Parse(textBox_PathID.Text);
                path[listView_Path.SelectedItems[0].Index].NameLen = 0;
                path[listView_Path.SelectedItems[0].Index].Name = Encoding.GetEncoding("GBK").GetBytes(textBox_Path.Text);

                listView_Path.SelectedItems[0].SubItems[0].Text = textBox_PathID.Text;
                listView_Path.SelectedItems[0].SubItems[1].Text = textBox_Path.Text;
            }
        }

        private void ListViewPathDoubleClick(object sender, EventArgs e)
        {
            if (listView_Path.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectFilePathFirst);
            else
            {
                textBox_PathID.Text = listView_Path.SelectedItems[0].SubItems[0].Text;
                textBox_Path.Text = listView_Path.SelectedItems[0].SubItems[1].Text;
            }
        }
    }
}
